import 'package:animal/network/api.dart';
import 'package:animal/ui/home/page/home_index_page.dart';
import 'package:animal/ui/login/NWApi.dart';
import 'package:animal/utils/index.dart';
import 'package:animal/utils/sputils/sp_config.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:multi_image_picker2/multi_image_picker2.dart';

class PerfectInfoController extends GetxController {
  //勾选按钮，默认false:男士  true：女士
  RxBool checkBtn = false.obs;

  TextEditingController controller = TextEditingController();

  final ImagePicker _picker = ImagePicker();

  RxString filePath = "".obs;

  RxList resultList = <Asset>[].obs;

  List<Asset> images = <Asset>[];

  ///勾选按钮事件
  void checkTap(bool? value) {
    debugPrint("$value");
    if (value!) {
      checkBtn.value = !checkBtn.value;
    }
  }

  ///前往商城首页
  void gotoHomeIndex() {
    Get.offAll(const HomeIndexPage());
  }

  ///完善用户资料
  void prefectInfo() {
    var putData = {
      "headImg": "",
      "nickName": controller.text,
      "sex": checkBtn.value?"F":"M",
    };
    SmartDialog.showLoading();
    HttpManager().post(NWApi.userAddInfo,queryParameters: putData).listen((value) {
      var userInfo = jsonDecode(SpConfig().getUserInfo()!);
      userInfo["nickName"] = putData["nickName"];
      userInfo["sex"] = putData["sex"];
      userInfo["headImg"] = putData["headImg"];
      SpConfig().setUserInfo(jsonEncode(userInfo));
      SmartDialog.dismiss();
      gotoHomeIndex();
    },onError: (err){
      SmartDialog.dismiss();
    });
  }

  ///获取图片
  selectedImage({int maxLength = 1}) async {
    XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    if (image != null) {
      debugPrint(image.path.toString());
      filePath.value = image.path;
    }
    // resultList.value = await MultiImagePicker.pickImages(
    //   maxImages: 300,
    //   enableCamera: true,
    //   selectedAssets: images,
    //   cupertinoOptions: const CupertinoOptions(
    //     takePhotoIcon: "chat",
    //     doneButtonTitle: "Fatto",
    //   ),
    //   materialOptions: const MaterialOptions(
    //     actionBarColor: "#abcdef",
    //     actionBarTitle: "Example App",
    //     allViewTitle: "All Photos",
    //     useDetailsView: false,
    //     selectCircleStrokeColor: "#000000",
    //   ),
    // );
  }

// /// 动态申请相机权限
// void requestPermission() async {
//   // 申请权限
//   bool hasLocationPermission = await requestLocationPermission();
//   if (hasLocationPermission) {
//     debugPrint("权限申请通过");
//     // scan();
//   } else {
//     SmartDialog.showToast("打开相机权限未开启，请进入应用权限设置允许相机即可使用");
//   }
// }
//
// /// 申请定位权限
// /// 授予定位权限返回true， 否则返回false
// Future<bool> requestLocationPermission() async {
//   //获取当前的权限
//   var status = await Permission.camera.status;
//   var storageStatus = await Permission.storage.status;
//   if (status == PermissionStatus.granted && storageStatus == PermissionStatus.granted) {
//     return true;
//   } else {
//     //未授权则发起一次申请
//     status = await Permission.camera.request();
//     storageStatus = await Permission.storage.request();
//     if (status == PermissionStatus.granted && storageStatus == PermissionStatus.granted) {
//       return true;
//     } else {
//       return false;
//     }
//   }
// }
}
